package ccpv;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;
import repast.simphony.context.Context;
import repast.simphony.context.space.gis.GeographyFactoryFinder;
import repast.simphony.dataLoader.ContextBuilder;
import repast.simphony.space.gis.Geography;
import repast.simphony.space.gis.GeographyParameters;

public class GISContextCreator implements ContextBuilder<GisAgent> {
	public Context<GisAgent> build(Context<GisAgent> context) {
		// Because the Geography acts as a container for the
		// GisAgent and its
		// associated Coordinate, you need to tell the Factory
		// what it will be
		// storing, a <GisAgent>.
		GeographyParameters<GisAgent> geoParams = new GeographyParameters<GisAgent>();
		Geography<GisAgent> geography = GeographyFactoryFinder
				.createGeographyFactory(null).createGeography("Geography",
						context, geoParams);
		// fac creates Coordinates to hold agent positions.
		GeometryFactory fac = new GeometryFactory();
		for (int i = 0; i < 10; i++) {
			// Added the agent to the context.
			GisAgent agent = new GisAgent();
			context.add(agent);
			// Then position the agent.
			// Since an agent is a POJO and its
			// position is held in the geometry
			// rather than the object itself, point
			// agent's position doesn't need
			// to be an attribute of the agent itself.
			Coordinate coord = new Coordinate(-103.823 + i / 100.0, 44.373);
			Point geom = fac.createPoint(coord);
			geography.move(agent, geom);
		}
		return context;
	}
}
